package com.example.androidlearning2200;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidlearning2200.adapter.EntryAdapter;
import com.example.androidlearning2200.bean.EntryBean;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private RecyclerView rvEntry;
    private List<EntryBean> datas = new ArrayList<>();
    // 当需要回传参数时，使用ActivityResultLauncher
    private ActivityResultLauncher<Intent> launcher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences sharedPreferences = getSharedPreferences("AppConfig", MODE_PRIVATE);
        boolean isLogin = sharedPreferences.getBoolean("is_login", false);
        if (!isLogin) {
            startActivity(new Intent(MainActivity.this, LoginActivity.class));
        }

        // 初始化启动器
        launcher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result.getResultCode() == Activity.RESULT_OK) {
                Intent data = result.getData();
                // 在这里处理回传的数据
                String result1 = data.getExtras().getString("result");
                Log.d("MainActivity", result1);
            }
        });
        setContentView(R.layout.activity_main);
        datas.add(new EntryBean("基本控件1", "文本、图片、输入框、按钮控件的使用", "8月28"));
        datas.add(new EntryBean("基本控件2", "单选、多选、进度条、拖动条、开关控件的使用", "9月4"));
        datas.add(new EntryBean("ScrollView", "横向、水平滚动控件的使用", "8月28"));
        datas.add(new EntryBean("WebView", "网页控件的使用，加载本地及网络上的网页", "8月28"));
        datas.add(new EntryBean("登录界面", "基本控件综合运用", "8月28"));
        datas.add(new EntryBean("引导页", "ViewPager实现引导页", "8月28"));
        datas.add(new EntryBean("RV-列表-微信聊天", "使用RecyclerView实现列表功能", "8月28"));
        datas.add(new EntryBean("RV-宫格-应用市场", "使用RecyclerView实现九宫格效果", "8月28"));
        datas.add(new EntryBean("Activity参数传递", "Activity之间传递参数、回传参数", "10月25"));
        datas.add(new EntryBean("Activity生命周期", "Activity从创建到销毁的全过程", "11月1日"));
        datas.add(new EntryBean("Activity启动模式", "Activity的四种启动模式", "11月1日"));
        datas.add(new EntryBean("CustomVideoView", "自定义视频播放组件", "11月8日"));
        datas.add(new EntryBean("TabLayout", "TabLayout实现选项卡分类分页", "11月15日"));
        datas.add(new EntryBean("BottomNavigationView", "底部导航视图，常用于搭建主界面", "11月24日"));
        datas.add(new EntryBean("Menu菜单的使用", "菜单的使用，包括popMenu的使用", "11月29日"));
        datas.add(new EntryBean("Dialog对话框使用", "对话框的使用，包括自定义对话框", "12月1日"));
        datas.add(new EntryBean("读取系统联系人", "Android读取系统联系人，涉及授权", "3月7日"));
        datas.add(new EntryBean("Notification", "Android发送通知，通知可以作为发开程序的入口", "3月14日"));
        datas.add(new EntryBean("拍照相册", "Android发送通知，通知可以作为发开程序的入口", "3月21日"));
        datas.add(new EntryBean("HttpURLConnection", "使用HttpURLConnection进行网络请求", "3月26日"));
        datas.add(new EntryBean("WanAndroid", "使用玩Android接口制作首页Banner", "4月23日"));
        datas.add(new EntryBean("SQLite数据库", "使用内置数据库SQLite存储数据", "4月28日"));
        datas.add(new EntryBean("BroadcastReceiver", "广播接收器的定义与使用", "5月9日"));
        datas.add(new EntryBean("Service", "后台服务的详细使用", "5月14日"));
        datas.add(new EntryBean("MusicPlayer", "音乐播放页面，实现播放音乐的功能", "5月21日"));
        datas.add(new EntryBean("抽屉菜单", "点击左上角菜单图标，打开侧滑抽屉菜单", "6月4日"));
        rvEntry = findViewById(R.id.rvEntry);
        rvEntry.setLayoutManager(new LinearLayoutManager(this));
        rvEntry.setAdapter(new EntryAdapter(datas) {
            @Override
            public void clickItem(int position) {
                switch (position) {
                    case 0:
                        startActivity(new Intent(MainActivity.this, Wdg1Activity.class));
                        break;
                    case 1:
                        startActivity(new Intent(MainActivity.this, Wdg2Activity.class));
                        break;
                    case 2:
                        startActivity(new Intent(MainActivity.this, ScrollViewActivity.class));
                        break;
                    case 3:
                        startActivity(new Intent(MainActivity.this, WebViewActivity.class));
                        break;
                    case 4:
                        startActivity(new Intent(MainActivity.this, LoginActivity.class));
                        break;
                    case 5:
                        startActivity(new Intent(MainActivity.this, WelcomeActivity.class));
                        break;
                    case 6:
                        startActivity(new Intent(MainActivity.this, RVListActivity.class));
                        break;
                    case 7:
                        startActivity(new Intent(MainActivity.this, AppRecentActivity.class));
                        break;
                    case 8:
                        // 启动Activity时传递参数
                        Intent intent = new Intent(MainActivity.this, ParamsActivity.class);
                        intent.putExtra("who", "我是竹汶霏");
                        intent.putExtra("from", "MainActivity");
                        launcher.launch(intent);
                        break;
                    case 9:
                        startActivity(new Intent(MainActivity.this, LifeCycleActivity.class));
                        break;
                    case 10:
                        startActivity(new Intent(MainActivity.this, FirstActivity.class));
                        break;
                    case 11:
                        startActivity(new Intent(MainActivity.this, CustomVideoActivity.class));
                        break;
                    case 12:
                        startActivity(new Intent(MainActivity.this, TabLayoutActivity.class));
                        break;
                    case 13:
                        startActivity(new Intent(MainActivity.this, BNVActivity.class));
                        break;
                    case 14:
                        startActivity(new Intent(MainActivity.this, MenuActivity.class));
                        break;
                    case 15:
                        startActivity(new Intent(MainActivity.this, DialogActivity.class));
                        break;
                    case 16:
                        startActivity(new Intent(MainActivity.this, ContactActivity.class));
                        break;
                    case 17:
                        startActivity(new Intent(MainActivity.this, NotificationActivity.class));
                        break;
                    case 18:
                        startActivity(new Intent(MainActivity.this, CameraActivity.class));
                        break;
                    case 19:
                        startActivity(new Intent(MainActivity.this, HttpActivity.class));
                        break;
                    case 20:
                        startActivity(new Intent(MainActivity.this, WanAndroidActivity.class));
                        break;
                    case 21:
                        startActivity(new Intent(MainActivity.this, SQLiteActivity.class));
                        break;
                    case 22:
                        startActivity(new Intent(MainActivity.this, ReceiverActivity.class));
                        break;
                    case 23:
                        startActivity(new Intent(MainActivity.this, ServiceActivity.class));
                        break;
                    case 24:
                        startActivity(new Intent(MainActivity.this, MusicActivity.class));
                        break;
                    case 25:
                        startActivity(new Intent(MainActivity.this, DrawerActivity.class));
                        break;
                }
            }
        });
    }

}